<?php

/**
* PHP获取字符串中英文混合长度
* @param $str string 字符串
* @param $$charset string 编码
* @return 返回长度，1中文=1位，2英文=1位
*/
function str_length($str,$charset='utf-8')
{
	if($charset=='utf-8') $str = iconv('utf-8','gb2312',$str);
	$num = strlen($str);
	$cnNum = 0;
	for($i=0;$i<$num;$i++){
		if(ord(substr($str,$i+1,1))>127){
			$cnNum++;
			$i++;
		}
	}
	$enNum = $num-($cnNum*2);
	$number = ($enNum/2)+$cnNum;
	return ceil($number);
}

//获取URL内容，解决地址出错问题
function file_get_content($url)
{
	if (function_exists('file_get_contents'))
	{
		$file_contents = @file_get_contents($url);
	}
	if ($file_contents == '')
	{
		$ch = curl_init();
		$timeout = 30;
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$file_contents = curl_exec($ch);
		curl_close($ch);
	}
	return $file_contents;
}

//获取视频缩略图
function getimg($link)
{
	$content = file_get_content($link);	//获取地址
	if ($content){
		$temp = substr($content, strpos($content,"screenshot="));
		$img = substr($temp, 11, strpos($temp,"\"")-11);
		return $img;
	}
	else
	{
		return 'error';
	}
}

//时间比较
function timeextend($nowtime,$addtime)
{
	$epoch_1=strtotime($nowtime);
	$epoch_2=strtotime($addtime);
	$diff_seconds=$epoch_1 - $epoch_2;
	$diff_weeks = floor($diff_seconds/604800);
	$diff_seconds -= $diff_weeks * 604800;
	$diff_days = floor($diff_seconds/86400);
	$diff_seconds -= $diff_days * 86400;
	$diff_hours= floor($diff_seconds / 3600);
	$diff_seconds -= $diff_hours * 3600;
	$diff_minutes = floor($diff_seconds / 60 );	
	return $diff_minutes;
}

//提示窗
function alert($message,$type)
{
	if($type==0)
	{
		echo '<script type="text/javascript" language="javascript" charset="utf-8">alert("'.$message.'");history.back();</script>';
	}
	else if($type==1)
	{
		echo '<script type="text/javascript" language="javascript" charset="utf-8">alert("'.$message.'");location.href="/work";</script>';
	}
	else
	{
		echo '<script type="text/javascript" language="javascript" charset="utf-8">alert("'.$message.'");location.href="/";</script>';
	}
}


?>